﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCFHS.Code;
using System.Data;
namespace SCFHS.Views
{
    public partial class Default : SysBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindReadyOrders();
                BindReadToPrintOrders();
                BindReadyToInstruction();
                BindReadyToFollowup();

            }
        }

        private void BindReadyOrders()
        {
            readyOrdersToComplete.Visible = user.IsInRole(UserRoles.CompleteOrder);
            grdReadyOrders.DataSource = controller.SelectOrderByState((int)OrderStates.ReadyTocomplete);
            grdReadyOrders.DataBind();
        }

        private void BindReadToPrintOrders()
        {

            readyOrdersToPrintOrders.Visible = user.IsInRole(UserRoles.PrintOrder);
            grdReadyToPrint.DataSource = controller.SelectOrderByState((int)OrderStates.Place);
            grdReadyToPrint.DataBind();

        }

        private void BindReadyToInstruction()
        {
            readyToInstruction.Visible = user.IsInRole(UserRoles.AddInstruction);
            grdInstruction.DataSource = controller.SelectOrderByState((int)OrderStates.Printed);
            grdInstruction.DataBind();
        }

        private void BindReadyToFollowup()
        {
            bool readyFollowUp = readyToFollowUp.Visible = user.IsInRole(UserRoles.AddFollowup);
            if (!readyFollowUp)
                return;
            DataTable waitingFollowUpDt = controller.SelectOrderByState((int)OrderStates.WaitingFollowUp);
            DataTable instructionsDt = controller.SelectOrderByState((int)OrderStates.Instructions);
            DataTable followUpDt = controller.SelectOrderByState((int)OrderStates.FollowUp);
            DataTable fullDt = new DataTable();

            if (waitingFollowUpDt != null)
                fullDt.Merge(waitingFollowUpDt);
            if (instructionsDt != null)
                fullDt.Merge(instructionsDt);
            if (followUpDt != null)
                fullDt.Merge(followUpDt);

            grdReadyToFollowup.DataSource = fullDt;
            grdReadyToFollowup.DataBind();
        }

        protected void grdReadyToPrint_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onClick"] = "location.href='Views/ReportsAndSearchs/Reports/OrderByNumber.aspx?orderId=" + DataBinder.Eval(e.Row.DataItem, "OrderNumber") + "'";
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';";
            }
        }

        protected void grdReadyOrders_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onClick"] = "location.href='Views/OrderManagements/CompleteOrder.aspx?orderId=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';";
            }
        }

        protected void grdInstruction_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onClick"] = "location.href='Views/OrderManagements/OrderInstruction.aspx?orderId=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';";
            }
        }

        protected void grdReadyToFollowup_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onClick"] = "location.href='Views/OrderManagements/Followup.aspx?orderId=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';";
            }
        }
    }
}